package org.dizem.groupon.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import org.apache.log4j.Logger;
import org.dizem.groupon.model.domain.ReplyEntity;
import org.dizem.groupon.service.ReplyService;

import java.util.List;

/**
 * User: dizem
 * Time: 11-7-20 下午7:20
 */
public class ReplyAction extends ActionSupport implements Preparable {

	private static Logger log = Logger.getLogger(ReplyAction.class);

	private ReplyService service;
	private ReplyEntity reply;
	private int itemId;
	private List<ReplyEntity> replies;
	private String username;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getItemId() {
		return itemId;
	}

	public void setItemId(int itemId) {
		this.itemId = itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = Integer.parseInt(itemId);
	}

	public List<ReplyEntity> getReplies() {
		return replies;
	}

	public void setReplies(List<ReplyEntity> replies) {
		this.replies = replies;
	}

	public ReplyService getService() {
		return service;
	}

	public void setService(ReplyService service) {
		this.service = service;
	}

	public ReplyEntity getReply() {
		return reply;
	}

	public void setReply(ReplyEntity reply) {
		this.reply = reply;
	}

	public String list() {
		replies = service.getByItemId(itemId);
		log.info("itemId=" + itemId + " load " + replies.size() + " replies");
		return SUCCESS;
	}

	public String add() {
		log.info("add reply + " + (reply == null));
		service.save(reply, username, itemId);
		return SUCCESS;
	}

	public void prepare() throws Exception {
		log.info("prepare itemId=" + itemId);
		replies = service.getByItemId(itemId);
	}

	public ReplyEntity getModel() {
		return reply;
	}
}
